package cn.chenchao.eshop.client;

import cn.chenchao.eshop.common.entity.Message;
import cn.chenchao.eshop.goods.action.GoodAction;
import cn.chenchao.eshop.goods.entity.Goods;
import util.JsonUtil;

import javax.sound.midi.Soundbank;
import java.util.ArrayList;
import java.util.List;

/**
 * @encoding : UTF-8
 * @Time : 2020/11/23 13:03
 * @Author : Chen Chao
 * @File : eshop.cn.chenchao.eshop.client.GoodClient.java
 * @Software : IntelliJ IDEA
 * @since : 1.8
 * 商品操作页面
 */
public class GoodClient extends Client{
    /**展示商品列表*/
    public  void showGoodsList(){
        System.out.println("欢迎来到商品列表页面：");
        /*1.向后台发送请求*/
        GoodAction goodAction = new GoodAction();
        String s = goodAction.showGoodsList();
        /*2.解析响应的字符串*/
        Message message = JsonUtil.json2Entity(s, Message.class);
        if(message.getMsgType().equals(Message.FAILURE)){
            System.out.println("信息获取失败！");
        }else if(message.getMsgType().equals(Message.SUCCESS)){
            Object obj = message.getObj();
            /*把obj强转成list类型*/
            List<?> list=(List<?>) obj;
            /*展示标头*/
            System.out.println("【商品列表】");
            System.out.println("编号\t商品名称\t单件\t库存");
            /*\t为一个Tab键的距离*/
            for (int i = 0; i <list.size(); i++) {
                /*从list里面取出每一个小的字符串转成goods对象*/
                Goods goods = JsonUtil.json2Entity(list.get(i).toString(), Goods.class);
                /*展示商品*/
                String name = goods.getName();
                String count = goods.getCount()+"";
                String price = goods.getPrice()+"";
                System.out.println((i+1)+"\t"+name+"\t\t"+price+"\t"+count);
            }



        }
        /*3.展示商品列表*/

    }
    /** 展示商品细节*/
    public  void showGoodsDetail(){

    }

    public static void main(String[] args) {
        GoodClient goodClient = new GoodClient();
        goodClient.showGoodsList();
    }
}
